home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / archiver / lzhsourc.lzh / LZH.SRC / LZARI.C < prev    next >
C/C++ Source or Header  |  1992-07-02  |  13KB  |  468 lines

  1. c/**************************************************************
  2.     LZARI.C -- A Data Compression Program
  3.     (tab = 4 spaces)
  4. ***************************************************************
  5.     4/7/1989 Haruhiko Okumura
  6.     Use, distribute, and modify this program freely.
  7.     Please send me your improved versions.
  8.         PC-VAN        SCIENCE
  9.         NIFTY-Serve    PAF01022
  10.         CompuServe    74050,1022
  11. **************************************************************/
  12. #define _shell
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <ctype.h>
  17.  
  18. /********** Bit I/O **********/
  19.  
  20. #ifdef _shell
  21.  extern FILE  *infile, *outfile;
  22.  extern unsigned long int  textsize, codesize;
  23. #else
  24. FILE  *infile, *outfile;
  25. unsigned long int  textsize = 0, codesize = 0;
  26. #endif
  27. unsigned long int printcount = 0;
  28.  
  29. void Error(char *message)
  30. {
  31.     printf("\n%s\n", message);
  32.     exit(EXIT_FAILURE);
  33. }
  34.  
  35. void PutBit(int bit)  /* Output one bit (bit = 0,1) */
  36. {
  37.     static unsigned int  buffer = 0, mask = 128;
  38.  
  39.     if (bit) buffer |= mask;
  40.     if ((mask >>= 1) == 0) {
  41.         putc(buffer, outfile);
  42.         buffer = 0;  mask = 128;  codesize++;
  43.     }
  44. }
  45.  
  46. void FlushBitBuffer(void)  /* Send remaining bits */
  47. {
  48.     int  i;
  49.  
  50.     for (i = 0; i < 7; i++) PutBit(0);
  51. }
  52.  
  53. int GetBit(void)  /* Get one bit (0 or 1) */
  54. {
  55.     static unsigned int  buffer, mask = 0;
  56.  
  57.     if ((mask >>= 1) == 0) {
  58.         buffer = getc(infile);    mask = 128;
  59.     }
  60.     return ((buffer & mask) != 0);
  61. }
  62.  
  63. /********** LZSS with multiple binary trees **********/
  64.  
  65. #define N         4096    /* size of ring buffer */
  66. #define F           60    /* upper limit for match_length */
  67. #define THRESHOLD    2   /* encode string into position and length
  68.                            if match_length is greater than this */
  69. #define NIL            N    /* index for root of binary search trees */
  70.  
  71. unsigned char  text_buf[N + F - 1];    /* ring buffer of size N,
  72.             with extra F-1 bytes to facilitate string comparison */
  73. int        match_position, match_length,  /* of longest match.  These are
  74.             set by the InsertNode() procedure. */
  75.         lson[N + 1], rson[N + 257], dad[N + 1];  /* left & right children &
  76.             parents -- These constitute binary search trees. */
  77.  
  78. void InitTree(void)  /* Initialize trees */
  79. {
  80.     int  i;
  81.  
  82.     /* For i = 0 to N - 1, rson[i] and lson[i] will be the right and
  83.        left children of node i.  These nodes need not be initialized.
  84.        Also, dad[i] is the parent of node i.  These are initialized to
  85.        NIL (= N), which stands for 'not used.'
  86.        For i = 0 to 255, rson[N + i + 1] is the root of the tree
  87.        for strings that begin with character i.  These are initialized
  88.        to NIL.  Note there are 256 trees. */
  89.  
  90.     for (i = N + 1; i <= N + 256; i++) rson[i] = NIL;    /* root */
  91.     for (i = 0; i < N; i++) dad[i] = NIL;    /* node */
  92. }
  93.  
  94. void InsertNode(int r)
  95.     /* Inserts string of length F, text_buf[r..r+F-1], into one of the
  96.        trees (text_buf[r]'th tree) and returns the longest-match position
  97.        and length via the global variables match_position and match_length.
  98.        If match_length = F, then removes the old node in favor of the new
  99.        one, because the old one will be deleted sooner.
  100.        Note r plays double role, as tree node and position in buffer. */
  101. {
  102.     int  i, p, cmp, temp;
  103.     unsigned char  *key;
  104.  
  105.     cmp = 1;  key = &text_buf[r];  p = N + 1 + key[0];
  106.     rson[r] = lson[r] = NIL;  match_length = 0;
  107.     for ( ; ; ) {
  108.         if (cmp >= 0) {
  109.             if (rson[p] != NIL) p = rson[p];
  110.             else {    rson[p] = r;  dad[r] = p;  return;  }
  111.         } else {
  112.             if (lson[p] != NIL) p = lson[p];
  113.             else {    lson[p] = r;  dad[r] = p;  return;  }
  114.         }
  115.         for (i = 1; i < F; i++)
  116.             if ((cmp = key[i] - text_buf[p + i]) != 0)  break;
  117.         if (i > THRESHOLD) {
  118.             if (i > match_length) {
  119.                 match_position = (r - p) & (N - 1);
  120.                 if ((match_length = i) >= F) break;
  121.             } else if (i == match_length) {
  122.                 if ((temp = (r - p) & (N - 1)) < match_position)
  123.                     match_position = temp;
  124.             }
  125.         }
  126.     }
  127.     dad[r] = dad[p];  lson[r] = lson[p];  rson[r] = rson[p];
  128.     dad[lson[p]] = r;  dad[rson[p]] = r;
  129.     if (rson[dad[p]] == p) rson[dad[p]] = r;
  130.     else               lson[dad[p]] = r;
  131.     dad[p] = NIL;  /* remove p */
  132. }
  133.  
  134. void DeleteNode(int p)    /* Delete node p from tree */
  135. {
  136.     int  q;
  137.  
  138.     if (dad[p] == NIL) return;  /* not in tree */
  139.     if (rson[p] == NIL) q = lson[p];
  140.     else if (lson[p] == NIL) q = rson[p];
  141.     else {
  142.         q = lson[p];
  143.         if (rson[q] != NIL) {
  144.             do {  q = rson[q];  } while (rson[q] != NIL);
  145.             rson[dad[q]] = lson[q];  dad[lson[q]] = dad[q];
  146.             lson[q] = lson[p];  dad[lson[p]] = q;
  147.         }
  148.         rson[q] = rson[p];  dad[rson[p]] = q;
  149.     }
  150.     dad[q] = dad[p];
  151.     if (rson[dad[p]] == p) rson[dad[p]] = q;
  152.     else               lson[dad[p]] = q;
  153.     dad[p] = NIL;
  154. }
  155.  
  156. /********** Arithmetic Compression **********/
  157.  
  158. /*  If you are not familiar with arithmetic compression, you should read
  159.         I. E. Witten, R. M. Neal, and J. G. Cleary,
  160.             Communications of the ACM, Vol. 30, pp. 520-540 (1987),
  161.     from which much have been borrowed.  */
  162.  
  163. #define M   15
  164.  
  165. /*    Q1 (= 2 to the M) must be sufficiently large, but not so
  166.     large as the unsigned long 4 * Q1 * (Q1 - 1) overflows.  */
  167.  
  168. #define Q1  (1UL << M)
  169. #define Q2  (2 * Q1)
  170. #define Q3  (3 * Q1)
  171. #define Q4  (4 * Q1)
  172. #define MAX_CUM (Q1 - 1)
  173.  
  174. #define N_CHAR    (256 - THRESHOLD + F)
  175.     /* character code = 0, 1, ..., N_CHAR - 1 */
  176.  
  177. unsigned long int  low = 0, high = Q4, value = 0;
  178. int  shifts = 0;  /* counts for magnifying low and high around Q2 */
  179. int  char_to_sym[N_CHAR], sym_to_char[N_CHAR + 1];
  180. unsigned int
  181.     sym_freq[N_CHAR + 1],  /* frequency for symbols */
  182.     sym_cum[N_CHAR + 1],   /* cumulative freq for symbols */
  183.     position_cum[N + 1];   /* cumulative freq for positions */
  184.  
  185. void StartModel(void)  /* Initialize model */
  186. {
  187.     int ch, sym, i;
  188.  
  189.     sym_cum[N_CHAR] = 0;
  190.     for (sym = N_CHAR; sym >= 1; sym--) {
  191.         ch = sym - 1;
  192.         char_to_sym[ch] = sym;    sym_to_char[sym] = ch;
  193.         sym_freq[sym] = 1;
  194.         sym_cum[sym - 1] = sym_cum[sym] + sym_freq[sym];
  195.     }
  196.     sym_freq[0] = 0;  /* sentinel (!= sym_freq[1]) */
  197.     position_cum[N] = 0;
  198.     for (i = N; i >= 1; i--)
  199.         position_cum[i - 1] = position_cum[i] + 10000 / (i + 200);
  200.             /* empirical distribution function (quite tentative) */
  201.             /* Please devise a better mechanism! */
  202. }
  203.  
  204. void UpdateModel(int sym)
  205. {
  206.     int i, c, ch_i, ch_sym;
  207.  
  208.     if (sym_cum[0] >= MAX_CUM) {
  209.         c = 0;
  210.         for (i = N_CHAR; i > 0; i--) {
  211.             sym_cum[i] = c;
  212.             c += (sym_freq[i] = (sym_freq[i] + 1) >> 1);
  213.         }
  214.         sym_cum[0] = c;
  215.     }
  216.     for (i = sym; sym_freq[i] == sym_freq[i - 1]; i--) ;
  217.     if (i < sym) {
  218.         ch_i = sym_to_char[i];      ch_sym = sym_to_char[sym];
  219.         sym_to_char[i] = ch_sym;  sym_to_char[sym] = ch_i;
  220.         char_to_sym[ch_i] = sym;  char_to_sym[ch_sym] = i;
  221.     }
  222.     sym_freq[i]++;
  223.     while (--i >= 0) sym_cum[i]++;
  224. }
  225.  
  226. static void Output(int bit)  /* Output 1 bit, followed by its complements */
  227. {
  228.     PutBit(bit);
  229.     for ( ; shifts > 0; shifts--) PutBit(! bit);
  230. }
  231.  
  232. void EncodeChar(int ch)
  233. {
  234.     int  sym;
  235.     unsigned long int  range;
  236.  
  237.     sym = char_to_sym[ch];
  238.     range = high - low;
  239.     high = low + (range * sym_cum[sym - 1]) / sym_cum[0];
  240.     low +=         (range * sym_cum[sym    ]) / sym_cum[0];
  241.     for ( ; ; ) {
  242.         if (high <= Q2) Output(0);
  243.         else if (low >= Q2) {
  244.             Output(1);  low -= Q2;    high -= Q2;
  245.         } else if (low >= Q1 && high <= Q3) {
  246.             shifts++;  low -= Q1;  high -= Q1;
  247.         } else break;
  248.         low += low;  high += high;
  249.     }
  250.     UpdateModel(sym);
  251. }
  252.  
  253. void EncodePosition(int position)
  254. {
  255.     unsigned long int  range;
  256.  
  257.     range = high - low;
  258.     high = low + (range * position_cum[position    ]) / position_cum[0];
  259.     low +=         (range * position_cum[position + 1]) / position_cum[0];
  260.     for ( ; ; ) {
  261.         if (high <= Q2) Output(0);
  262.         else if (low >= Q2) {
  263.             Output(1);  low -= Q2;    high -= Q2;
  264.         } else if (low >= Q1 && high <= Q3) {
  265.             shifts++;  low -= Q1;  high -= Q1;
  266.         } else break;
  267.         low += low;  high += high;
  268.     }
  269. }
  270.  
  271. void EncodeEnd(void)
  272. {
  273.     shifts++;
  274.     if (low < Q1) Output(0);  else Output(1);
  275.     FlushBitBuffer();  /* flush bits remaining in buffer */
  276. }
  277.  
  278. int BsrcSym(unsigned int x)
  279.     /* 1      if x >= sym_cum[1],
  280.        N_CHAR if sym_cum[N_CHAR] > x,
  281.        i such that sym_cum[i - 1] > x >= sym_cum[i] otherwise */
  282. {
  283.     int i, j, k;
  284.  
  285.     i = 1;    j = N_CHAR;
  286.     while (i <